package com.purebang.spaceinvaders;

import java.awt.Rectangle;

public class LaserCannon {

	public static final int HEIGHT = 30;
	public static final int WIDTH = 40;
	
	//Listener that records player keyboard inputs
	private PlayerInput input;
	private Rectangle hitbox;
	private int xSpeed = 5;
	
	//true whenever player presses button to fire laser
	private boolean fireLaser;
	
	public LaserCannon(int x, int y, PlayerInput input) {
		this.input = input;
		hitbox = new Rectangle(WIDTH, HEIGHT);
		hitbox.x = x;
		hitbox.y = y;
		fireLaser = false;
	}
	
	public void update() {
		if (input.isLeftPressed() && !input.isRightPressed()) {
			moveLeft();
		}
		
		if (!input.isLeftPressed() && input.isRightPressed()) {
			moveRight();
		}
		
		if(input.isFirePressed()) {
			fireLaser = true;
		}else
			fireLaser = false;
	}
	
	private void moveLeft() {
		
		//check to make sure player isn't moving "left of screen"
		if ((hitbox.x - xSpeed) > 0)
			hitbox.x = hitbox.x - xSpeed;
		else
			hitbox.x = 0;
	}
	
	private void moveRight() {
		
		//check to make sure player isn't moving "right of screen"
		if ((hitbox.x + xSpeed + hitbox.width) < GameManager.LEVEL_WIDTH)
			hitbox.x = hitbox.x + xSpeed;
		else
			hitbox.x = GameManager.LEVEL_WIDTH - hitbox.width - 1;
	}
	
	public boolean isLaserFired() {
		return fireLaser;
	}

	public Rectangle getHitbox() {
		return hitbox;
	}

}
